import Cookies from 'js-cookie'
import {isBlank} from "@/utils/math";

// App
const sidebarStatusKey = 'sidebar_status';
export const getSidebarStatus = () => Cookies.get(sidebarStatusKey);
export const setSidebarStatus = (sidebarStatus: string) => Cookies.set(sidebarStatusKey, sidebarStatus);

const languageKey = 'language';
export const getLanguage = () => Cookies.get(languageKey);
export const setLanguage = (language: string) => Cookies.set(languageKey, language);

const sizeKey = 'size';
export const getSize = () => Cookies.get(sizeKey);
export const setSize = (size: string) => Cookies.set(sizeKey, size);

// User
export const UserKey = {
    Token: 'Token',
    RefreshToken: 'RefreshToken',
    UserInfo: 'UserInfo',
    MerchantId: 'MerchantId',
    Routes: 'Routes',
    AllMenu: 'AllMenu',
    Role: 'Role',
    Menu: 'RoleMenu',

};

// Dict
export const DictKey = {
    OpenErrorLog: 'OpenErrorLog',
    OpenWarnLog: 'OpenWarnLog',
    HtmlVersion: 'HtmlVersion',
    ApiVersion: 'ApiVersion',
    BaseImageUrl: 'BaseImageUrl',
    SocketUrl: 'SocketUrl',
    showLog: 'showLog',
};
// Rob
export const OtherKey = {
    Rob: 'Rob',
};

export const setRob = (rob: string) => localStorage.setItem(OtherKey.Rob, rob);
export const getRob = (): any => localStorage.getItem(OtherKey.Rob);
export const removeRob = () => localStorage.removeItem(OtherKey.Rob);
//
export const setShowLog = (isSHow: any) => {
    if (isSHow) {
        localStorage.setItem(DictKey.showLog, isSHow.toString());
    } else {
        localStorage.setItem(DictKey.showLog, "false");
    }
};
export const showLog = (): any => localStorage.getItem(DictKey.showLog);
export const removeShowLog = () => localStorage.removeItem(DictKey.showLog);
//
export const setImageUrl = (baseImageUrl: string) => localStorage.setItem(DictKey.BaseImageUrl, baseImageUrl);
export const getImageUrl = (): any => localStorage.getItem(DictKey.BaseImageUrl);
export const removeImageUrl = () => localStorage.removeItem(DictKey.BaseImageUrl);
//
export const setSocketUrl = (socketUrl: string) => localStorage.setItem(DictKey.SocketUrl, socketUrl);
export const getSocketUrl = (): any => localStorage.getItem(DictKey.SocketUrl);
export const removeSocketUrl = () => localStorage.removeItem(DictKey.SocketUrl);
//
export const setRoleInfo = (role: string) => localStorage.setItem(UserKey.Role, role);
export const getRoleInfo = (): any => localStorage.getItem(UserKey.Role);
export const removeRoleInfo = () => localStorage.removeItem(UserKey.Role);
//
export const setMenus = (menus: string) => localStorage.setItem(UserKey.Menu, menus);
export const getMenus = () => {
    try {
        if (isBlank(localStorage.getItem(UserKey.Menu))) {
            return [];
        }
        return JSON.parse(localStorage.getItem(UserKey.Menu) as any);
    } catch (e) {
        return [];
    }
};
export const removeMenus = () => localStorage.removeItem(UserKey.Menu);
//
export const setHtmlVersion = (version: string) => localStorage.setItem(DictKey.HtmlVersion, version);
export const getHtmlVersion = () => {
    if (localStorage.getItem(DictKey.HtmlVersion)) {
        return localStorage.getItem(DictKey.HtmlVersion);
    } else {
        return "0";
    }
};


//
export const setOpenErrorLog = (value: string) => localStorage.setItem(DictKey.OpenErrorLog, value);
export const getOpenErrorLog = () => {
    let item = localStorage.getItem(DictKey.OpenErrorLog);
    if (item) {
        return item == "true";
    }
    return false;
};
//
export const setOpenWarnLog = (value: string) => localStorage.setItem(DictKey.OpenWarnLog, value);
export const getOpenWarnLog = () => {
    let item = localStorage.getItem(DictKey.OpenWarnLog);
    if (item) {
        return item == "true";
    }
    return false;
};

//
export const setApiVersion = (version: string) => localStorage.setItem(DictKey.ApiVersion, version);
export const getApiVersion = () => {
    if (localStorage.getItem(DictKey.ApiVersion)) {
        return localStorage.getItem(DictKey.ApiVersion);
    } else {
        return "0";
    }
};
export const removeHtmlVersion = () => localStorage.removeItem(DictKey.HtmlVersion);

//
export const setToken = (token: string) => localStorage.setItem(UserKey.Token, token);
export const getToken = () => localStorage.getItem(UserKey.Token);
export const removeToken = () => localStorage.removeItem(UserKey.Token);
//
export const setRefreshToken = (refresh_token: string) => localStorage.setItem(UserKey.RefreshToken, refresh_token);
export const getRefreshToken = () => localStorage.getItem(UserKey.RefreshToken);
export const removeRefreshToken = () => localStorage.removeItem(UserKey.RefreshToken);
//
export const setUserInfo = (user_info: string) => localStorage.setItem(UserKey.UserInfo, user_info);
export const getUserInfo = () => localStorage.getItem(UserKey.UserInfo) as string;
export const removeUserInfo = () => localStorage.getItem(UserKey.UserInfo);
export const getUserId = () => JSON.parse(getUserInfo() as any).id;
export const getStoreId = () => JSON.parse(getUserInfo() as any).storeId;


//
export const setMerchantId = (merchantId: string) => localStorage.setItem(UserKey.MerchantId, merchantId);
export const getMerchantId = () => {
    if (isBlank(localStorage.getItem(UserKey.MerchantId))) {
        return "0";
    } else {
        return localStorage.getItem(UserKey.MerchantId);
    }
};


//
export const removeAllCache = () => {
    for (let key in UserKey) {
        localStorage.removeItem((UserKey as any)[key])
    }
};





